package com.xiaoshuidi.cloud.module.finance.controller.app.invoice;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.security.core.LoginUser;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.finance.controller.admin.invoice.vo.AppInvoiceIssueReqVO;
import com.xiaoshuidi.cloud.module.finance.controller.app.invoice.vo.AppBaseInvoiceVO;
import com.xiaoshuidi.cloud.module.finance.controller.app.invoice.vo.AppFinanceInvoicePageReqVO;
import com.xiaoshuidi.cloud.module.finance.convert.finance.InvoiceConvert;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.invoice.InvoiceDO;
import com.xiaoshuidi.cloud.module.finance.dal.mysql.invoice.BaseInvoiceMapper;
import com.xiaoshuidi.cloud.module.finance.enums.ErrorCodeConstants;
import com.xiaoshuidi.cloud.module.finance.enums.finance.FinanceCustomerTypeEnum;
import com.xiaoshuidi.cloud.module.finance.service.invoice.BaseInvoiceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

@Tag(name = "租客 APP - 发票")
@Slf4j
@RestController
@RequestMapping("/finance/app/baseInvoice")
public class AppBaseInvoiceController {

    @Resource
    private BaseInvoiceService baseInvoiceService;
    @Resource
    private BaseInvoiceMapper invoiceMapper;

    /**
     * 租客申请开票
     * @return
     */
    @PostMapping("/appIssueInvoices")
    @PermitAll
    @Operation(summary = "租客申请开票")
    public CommonResult<String> appIssueInvoices(@Valid @RequestBody AppInvoiceIssueReqVO reqVO) {
        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        return CommonResult.success(baseInvoiceService.appIssueInvoices(user,reqVO));
    }

    @GetMapping("/page")
    @PermitAll
    @Operation(summary = "开票历史")
    public CommonResult<PageResult<AppBaseInvoiceVO>> page(AppFinanceInvoicePageReqVO reqVO) {
        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        reqVO.setCustId(user.getId());
        reqVO.setCustType(FinanceCustomerTypeEnum.INDIVIDUAL.name());
        PageResult<InvoiceDO> page = invoiceMapper.selectPage(reqVO, new QueryWrapper<InvoiceDO>()
                .eq(StringUtils.hasText(reqVO.getStatus()), "status", reqVO.getStatus())
                .eq("cust_id", reqVO.getCustId())
                .eq("cust_type", reqVO.getCustType()));
        return CommonResult.success(InvoiceConvert.INSTANCE.convertByAppBaseInvoicePage(page));
    }

    @GetMapping("/get")
    @PermitAll
    @Operation(summary = "开票详情")
    public CommonResult<AppBaseInvoiceVO> detail(@RequestParam("invoiceId") Long invoiceId) {
        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        InvoiceDO invoiceDO = invoiceMapper.selectById(invoiceId);
        if (invoiceDO == null || !invoiceDO.getCustId().equals(user.getId())
                || !FinanceCustomerTypeEnum.INDIVIDUAL.name().equals(invoiceDO.getCustType())) {
            throw new ServiceException(ErrorCodeConstants.FINANCE_INVOICE_NOT_EXISTS);
        }
        return CommonResult.success(InvoiceConvert.INSTANCE.convertByAppBaseInvoice(invoiceDO));
    }
}
